package com.fiberhome.boot.demo.secondary.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fiberhome.boot.demo.primary.domain.Demo;
import com.fiberhome.boot.demo.secondary.dao.SecondaryRepository;
import com.fiberhome.boot.demo.secondary.domain.PcpRegion;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value="/secondary")
@Api(value = "oracle数据源测试", description="oracle数据源测试", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SecondaryController {

	@Autowired
	SecondaryRepository repository;
	
	@ApiOperation(value="保存地区信息", notes="保存地区信息")
	@RequestMapping(value="/saveRegion",method=RequestMethod.POST)
	public String saveUsers(@RequestBody PcpRegion region){
		repository.save(region);
		return "save sucusess";
	}
	
	@ApiOperation(value="根据id地区信息", notes="根据ID地区信息")
	@RequestMapping(value="/regionById",method=RequestMethod.GET)
	public PcpRegion getUserById(@RequestParam String id){
		return repository.findById(id);
	}
	
	@ApiOperation(value="根据parent_id地区信息", notes="根据parent_id地区信息")
	@RequestMapping(value="/regionByParentId",method=RequestMethod.GET)
	public List<PcpRegion> findByParentId(@RequestParam String id){
		System.out.println("id is +++++"+id);
		return repository.findByParentId(id);
	}
	
	@ApiOperation(value="获取地区记录总数", notes="获取地区记录总数")
	@RequestMapping(value="/regionCount",method=RequestMethod.GET)
	public long getCount(){
		return repository.count();
	}
	
	@ApiOperation(value="获取所有地区记录", notes="获取所有地区记录")
	@RequestMapping(value="/regions",method=RequestMethod.GET)
	public Iterable<PcpRegion> getAll(){
		return repository.findAll();
	}
}
